**************************************************************************************
* Extra analysis: Results available upon request for the concurrent election project**
**************************************************************************************

cls
clear all
clear matrix
set more off
capture log close

log using "C:/Userdata/Shared/Logs/Concurrent elections/extra.text", replace text 

cd "C:/Userdata/Shared/Dofiles/"

* Path directories*
global dodatadf "DoData/Concurrent elections"									// DoData dofiles
global doanalysisdf "DoAnalysis/Concurrent elections"							// Doanalysis dofiles
global usingdata "E:/ProjData/Concurrent elections/"							// Using data directory
global rawdata "D:/SCB_ConPol/Stata"											// Raw data directory
global output "C:/Userdata/Shared/Output/Concurrent elections/extra"			// Output directory	
do "$dodatadf/programs.do"		


********************************************************************************
* LOAD MAIN DATA SET************************************************************
********************************************************************************

use "$usingdata/finaldata/finaldata_immigrants.dta", clear

* Esttab setup*
global esttab "replace label se nogaps b(3) compress nostar booktabs nonotes varlabels(_cons "Constant")"

* Covariates macros *
global ICovs "ehat_pre_ink ehat_pre_emp ehat_yeduc sex children age bigcity"


*** Spill-over analysis ***

** Randwidth graphs **

clear matrix
forvalues k = 10(1)79 {															
	preserve
	rdrobust voted_local_spouse days_treat_enrolled if (days_treat_enrolled > 7 | days_treat_enrolled <=0) & year_ctz == 1994, h(`k') kernel(uniform)   
	matrix define a=e(b)														
	scalar define a1=a[1,1]
	matrix define v=e(V)
	scalar define v1=sqrt(v[1,1])												
	scalar define upper=a1+1.96*v1											
	scalar define lower=a1-1.96*v1
	scalar define bandwidth=`k'
	matrix define rd=[nullmat(rd)\bandwidth,a1,upper,lower]
	restore
}
svmat rd
preserve
keep rd1 rd2 rd3 rd4 
drop if rd1 == .
graph twoway (line rd2 rd1, lcolor(black)) (line rd3 rd1, lpattern(dash) lcolor(gs8)) (line rd4 rd1, lpattern(dash) lcolor(gs8)), graphregion(color(white)) xlabel(7(6)79) legend(order(1 "Point estimate sharp RD" 2 "95 % CI") region(col(white))) ytitle("Estimated coefficient") xtitle("Bandwidth")
graph export "$output/spouse1994.pdf", replace 
restore
drop rd*

clear matrix
forvalues k = 17(1)79 {	/*Not enough observations to run this with fewer observations*/														
	preserve
	rdrobust voted_local_spouse days_treat_enrolled if (days_treat_enrolled > 7 | days_treat_enrolled <=0) & year_ctz == 2010, h(30 `k') kernel(uniform)   
	matrix define a=e(b)														
	scalar define a1=a[1,1]
	matrix define v=e(V)
	scalar define v1=sqrt(v[1,1])												
	scalar define upper=a1+1.96*v1											
	scalar define lower=a1-1.96*v1
	scalar define bandwidth=`k'
	matrix define rd=[nullmat(rd)\bandwidth,a1,upper,lower]
	restore
}
svmat rd
preserve
keep rd1 rd2 rd3 rd4 
drop if rd1 == .
graph twoway (line rd2 rd1, lcolor(black)) (line rd3 rd1, lpattern(dash) lcolor(gs8)) (line rd4 rd1, lpattern(dash) lcolor(gs8)), graphregion(color(white)) xlabel(7(6)79) legend(order(1 "Point estimate sharp RD" 2 "95 % CI") region(col(white))) ytitle("Estimated coefficient") xtitle("Bandwidth")
graph export "$output/spouse2010.pdf", replace 
restore
drop rd*

clear matrix
forvalues k = 10(1)79 {															
	preserve
	rdrobust turnimmig_local_turnout days_treat_enrolled if (days_treat_enrolled > 7 | days_treat_enrolled <=0) & year_ctz == 1994, h(`k') kernel(uniform)   
	matrix define a=e(b)														
	scalar define a1=a[1,1]
	matrix define v=e(V)
	scalar define v1=sqrt(v[1,1])												
	scalar define upper=a1+1.96*v1											
	scalar define lower=a1-1.96*v1
	scalar define bandwidth=`k'
	matrix define rd=[nullmat(rd)\bandwidth,a1,upper,lower]
	restore
}
svmat rd
preserve
keep rd1 rd2 rd3 rd4 
drop if rd1 == .
graph twoway (line rd2 rd1, lcolor(black)) (line rd3 rd1, lpattern(dash) lcolor(gs8)) (line rd4 rd1, lpattern(dash) lcolor(gs8)), graphregion(color(white)) xlabel(7(6)79) legend(order(1 "Point estimate sharp RD" 2 "95 % CI") region(col(white))) ytitle("Estimated coefficient") xtitle("Bandwidth")
graph export "$output/immig1994.pdf", replace 
restore
drop rd*

clear matrix
forvalues k = 10(1)79 {															
	preserve
	rdrobust turnimmig_local_turnout days_treat_enrolled if (days_treat_enrolled > 7 | days_treat_enrolled <=0) & year_ctz == 2010, h(30 `k') kernel(uniform)   
	matrix define a=e(b)														
	scalar define a1=a[1,1]
	matrix define v=e(V)
	scalar define v1=sqrt(v[1,1])												
	scalar define upper=a1+1.96*v1											
	scalar define lower=a1-1.96*v1
	scalar define bandwidth=`k'
	matrix define rd=[nullmat(rd)\bandwidth,a1,upper,lower]
	restore
}
svmat rd
preserve
keep rd1 rd2 rd3 rd4 
drop if rd1 == .
graph twoway (line rd2 rd1, lcolor(black)) (line rd3 rd1, lpattern(dash) lcolor(gs8)) (line rd4 rd1, lpattern(dash) lcolor(gs8)), graphregion(color(white)) xlabel(7(6)79) legend(order(1 "Point estimate sharp RD" 2 "95 % CI") region(col(white))) ytitle("Estimated coefficient") xtitle("Bandwidth")
graph export "$output/immig2010.pdf", replace 
restore
drop rd*



***
clear all
capture log close
